#include "mypushbutton.h"
#include <QDebug>
#include <QPropertyAnimation>
MyPushButton::MyPushButton(QString normalImg,QString pressImg){
    this->normalPath=normalImg;
    this->pressPath=pressImg;
    //创建QPixmap
    QPixmap pix;
    bool ret=pix.load(this->normalPath);
    if(!ret){
        QString str=QString("图片加载失败，失败的路径路径是：%1!").arg(this->normalPath);
qDebug()<<str;
    }
    //缩放图片
    pix=pix.scaled(pix.width()*1.7,pix.height()*1.7);
//设置按钮固定尺寸
    this->setFixedSize(pix.width(),pix.height());
//边框消除
            this->setStyleSheet("QPushButton{border:0px;}");
    //图标
    this->setIcon(pix);
    //图标大小
    this->setIconSize(QSize(pix.width(),pix.height()));

}
//向下跳跃
void MyPushButton::zoom1()
{//创建动画对象
    QPropertyAnimation * animation=new QPropertyAnimation(this,"geometry");
    //设置动画间隔
    animation->setDuration(200);
    //设置启示位置
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
//设置结束位置
    animation->setEndValue(QRect(this->x(),this->y()+20,this->width(),this->height()));
//设置动画曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);
//执行动画
    animation->start(QAbstractAnimation::DeleteWhenStopped);

}

void MyPushButton::zoom2()
{//创建动画对象
    QPropertyAnimation * animation=new QPropertyAnimation(this,"geometry");
    //设置动画间隔
    animation->setDuration(200);
    //设置启示位置
    animation->setStartValue(QRect(this->x(),this->y()+20,this->width(),this->height()));
//设置结束位置
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
//设置动画曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);
//执行动画
    animation->start(QAbstractAnimation::DeleteWhenStopped);


}

void MyPushButton::mousePressEvent(QMouseEvent *e)
{
    //按下路径不为空，说明要切换图片
    if(this->pressPath!=""){
        //创建QPixmap
        QPixmap pix;
        bool ret=pix.load(this->pressPath);
        if(!ret){
            QString str=QString("图片加载失败，失败的路径路径是：%1!").arg(this->pressPath);
    qDebug()<<str;
        }
    //设置按钮固定尺寸
        this->setFixedSize(pix.width(),pix.height());
    //边框消除
                this->setStyleSheet("QPushButton{border:0px;}");
        //图标
        this->setIcon(pix);
        //图标大小
        this->setIconSize(QSize(pix.width(),pix.height()));}
    //其他事情交给父类处理
    QPushButton::mousePressEvent(e);
}

void MyPushButton::mouseReleaseEvent(QMouseEvent *e)
{
    //按下路径不为空，说明要还原图片
    if(this->pressPath!=""){
        //创建QPixmap
        QPixmap pix;
        bool ret=pix.load(this->normalPath);
        if(!ret){
            QString str=QString("图片加载失败，失败的路径路径是：%1!").arg(this->normalPath);
    qDebug()<<str;
        }
    //设置按钮固定尺寸
        this->setFixedSize(pix.width(),pix.height());
    //边框消除
                this->setStyleSheet("QPushButton{border:0px;}");
        //图标
        this->setIcon(pix);
        //图标大小
        this->setIconSize(QSize(pix.width(),pix.height()));}
    //其他事情交给父类处理
    QPushButton::mouseReleaseEvent(e);
}
